//
//  WmcTeamMemberAlbumListCell.swift
//  YPWatermarkCamera
//
//  Created by yupao_ios_macmini06 on 2022/9/15.
//

import UIKit

class WmcTeamMemberAlbumListCell: WmcBaseTableCell {
    override func makeUI() {
        contentView.addSubview(imgView)
        contentView.addSubview(videoIconImageView)
        contentView.addSubview(memberNameLabel)
        contentView.addSubview(photosCountLabel)
        contentView.addSubview(arrowImageView)
        contentView.addSubview(lineView)

        imgView.snp.makeConstraints { make in
            make.top.equalTo(16)
            make.left.equalTo(16)
            make.bottom.equalTo(-16)
            make.width.height.equalTo(56)
        }

        videoIconImageView.snp.makeConstraints { make in
            make.center.equalTo(imgView.snp.center)
            make.width.height.equalTo(28)
        }

        photosCountLabel.snp.makeConstraints { make in
            make.bottom.equalTo(imgView.snp.bottom).offset(-4)
            make.left.equalTo(memberNameLabel.snp.left)
        }

        arrowImageView.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalTo(-16)
            make.width.height.equalTo(20)
        }

        arrowImageView.contentMode = .scaleAspectFill
        arrowImageView.image = "teamAlbumRightArrow".wmc_image

        memberNameLabel.snp.makeConstraints { make in
            make.top.equalTo(imgView.snp.top).offset(4)
            make.left.equalTo(imgView.snp.right).offset(12)
            make.right.lessThanOrEqualTo(arrowImageView.snp.left).offset(-8)
        }

        lineView.snp.makeConstraints { make in
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.bottom.equalToSuperview()
            make.height.equalTo(1)
        }

        lineView.backgroundColor = UIColor.wmc_grayH19_F5F5F5
    }

    func configure(model: WmcMediaClassifyAlbumMemberModel) {
        // let member = WmcTeamMember.init(name: model.remark, img: model.url)
        // self.imgView.member = member
        memberNameLabel.text = model.remark
        photosCountLabel.text = "\(model.num)张"

        if model.loadStatus == .loadFailure {
            imgView.image = "photo_load_failure".wmc_image
            return
        }

        var params = ""

        if model.resourceType == 0 {
            params = "x-oss-process=image/resize,h_200,m_lfit"
            videoIconImageView.isHidden = true
        } else if model.resourceType == 1 {
            params = "x-oss-process=video/snapshot,t_7000,ar_auto,f_jpg,m_fast"
            videoIconImageView.isHidden = false
        }

        var url = "\(model.url)"
        var symbol = "?"
        if url.contains(symbol) {
            symbol = "&"
        }
        url.append(contentsOf: "\(symbol)\(params)")

        if let imgUrl = URL(string: url) {
            let urlParts = model.url.components(separatedBy: "?")
            var key = model.url
            if urlParts.count == 2 {
                key = urlParts[0]
            }
            key.append("/thumb")
            imgView.kf.setImage(with: .network(Kingfisher.ImageResource(downloadURL: imgUrl, cacheKey: key)), placeholder: "image_placeholder".wmc_image, completionHandler: { [weak self] r in
                guard let weakSelf = self else { return }
                switch r {
                case .success:
                    model.loadStatus = .loadsuccess
                case .failure:
                    model.loadStatus = .loadFailure
                    weakSelf.imgView.image = "photo_load_failure".wmc_image
                }
            })
        }
    }

    func configure(model: WmcMediaPhotographStatisticsModel) {
        // let member = WmcTeamMember.init(name: nil, img: model.url)
        // self.imgView.member = member
        memberNameLabel.text = model.name
        photosCountLabel.text = "\(model.num)张"

        if model.loadStatus == .loadFailure {
            imgView.image = "photo_load_failure".wmc_image
            return
        }

        var params = ""

        if model.resourceType == 0 {
            params = "x-oss-process=image/resize,h_200,m_lfit"
            videoIconImageView.isHidden = true
        } else if model.resourceType == 1 {
            params = "x-oss-process=video/snapshot,t_7000,ar_auto,f_jpg,m_fast"
            videoIconImageView.isHidden = false
        }

        var url = "\(model.url)"
        var symbol = "?"
        if url.contains(symbol) {
            symbol = "&"
        }
        url.append(contentsOf: "\(symbol)\(params)")

        if let imgUrl = URL(string: url) {
            let urlParts = model.url.components(separatedBy: "?")
            var key = model.url
            if urlParts.count == 2 {
                key = urlParts[0]
            }
            key.append("/thumb")
            imgView.kf.setImage(with: .network(Kingfisher.ImageResource(downloadURL: imgUrl, cacheKey: key)), placeholder: "image_placeholder".wmc_image, completionHandler: { [weak self] r in
                guard let weakSelf = self else { return }
                switch r {
                case .success:
                    model.loadStatus = .loadsuccess
                case .failure:
                    model.loadStatus = .loadFailure
                    weakSelf.imgView.image = "photo_load_failure".wmc_image
                }
            })
        }

        // titleLabel.text = model.takeTimeStr
    }

    lazy var imgView: UIImageView = {
        let view = UIImageView()
        return view
    }()

    lazy var memberNameLabel: UILabel = {
        let view = UILabel()
        view.textColor = UIColor.wmc_grayH2_323233
        view.font = 17.yp.uiFont
        view.text = "成员名称"
        return view
    }()

    lazy var photosCountLabel: UILabel = {
        let view = UILabel()
        view.textColor = UIColor.wmc_grayH5_606066
        view.font = 15.yp.uiFont
        view.text = "0张"
        return view
    }()

    private lazy var videoIconImageView: UIImageView = {
        let view = UIImageView(image: "player_play_center".wmc_image)
        view.isHidden = true
        return view
    }()
}
